﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using XMLGener.Interfaces;
using XMLGener.Utils;
using XMLGener.Data;

namespace XBasicModules
{
    /// <summary>
    /// 
    /// </summary>
    public class XPathFromGeneratedFile : ModuleBase, XMLGenerModuleInterface
    {

        #region XMLGenerModuleInterface Members

        /// <summary>
        /// Generate next number
        /// </summary>
        /// <returns>number</returns>
        public virtual string generate()
        {
            if (list == null || list.Count <1)
                return null;

            string result = list[listIterator++].InnerText;
            if (listIterator >= list.Count)
                listIterator = 0;
            return result;

        }

        /// <summary>
        /// Retrurn definitnion of this module
        /// </summary>
        /// <returns>xml data</returns>
        public virtual string getDefinition()
        {
            return
              "<function>" +
              "   <name>Xpath from generated file</name>" +
              "   <id>xpahtFromGenerated</id>" +
              "   <type>string</type>" +
              "   <attributes>" +
              "     <attribute>" +
              "         <name>Source file</name>" +
              "         <id>sourceId</id>" +
              "         <type>generatedFile</type>" +
              "      </attribute>" +
              "      <attribute>" +
              "         <name>Path</name>" +
              "         <id>xpath</id>" +
              "         <type>text</type>" +
              "      </attribute>" +
              "   </attributes>" +
              "</function>";
        }

        private XMLGenerDocument parentDocument = null;
        private string sourceId = null;
        private string xpathString = null;
        XmlDocument sourceXmlDocument = null;
        XmlNodeList list = null;
        int listIterator = 0;
        /// <summary>
        /// Set attributes for this module before generating.
        /// </summary>
        /// <param name="xmlParams"></param>
        /// <param name="parentDocument"></param>
        public virtual void setParameters(string xmlParams, XMLGenerDocument parentDocument)
        {
            this.parentDocument = parentDocument;
            if (!this.checkSettingsElement(xmlParams, parentDocument))
                return;
            listIterator = 0;
            this.sourceId = this.getParam("sourceId");
            this.xpathString = this.getParam("xpath");
            string sourceFile = this.parentDocument.getPathToGeneratedFile(sourceId);
            if (sourceXmlDocument == null)
                sourceXmlDocument = new XmlDocument();
            sourceXmlDocument.Load(sourceFile);
            XmlElement elem = (XmlElement)sourceXmlDocument.DocumentElement;
            if (elem != null)
            {
                list = elem.SelectNodes(xpathString);
                
            }
            else
            {
                list = null;
            }


        }

        #endregion
    }
}
